/*
客户端网络通信处理模块，主要处理与服务器通信的业务逻辑
*/
require("pomelo");
var confige = require("confige");

// require('pomelo-cocos2d-js');
// cc.Class({
//     extends: cc.Component,
//     properties: {
//     },
//     onLoad: function () {
//     },
// });

var pClient = function(){
    var pomelo = window.pomelo;

    var clientData = {
        diamond : 0,
        head : 0,
        nickName : "nick",
        uid : 1003
    }
    pomelo.clientScene = {};
    pomelo.clientScene.connectCallBack = function(){};

    pomelo.on("onMessage",function(data) {
        console.log("onMessage");
        console.log(data);
        pomelo.dealWithOnMessage(data);
    });

    pomelo.on("onTips",function(data) {
        console.log("onTips!!!!!"+data);
        window.clientGlobal.showTipsWord(data);
    });

    pomelo.dealWithOnMessage = function(data){
        switch(data.cmd)
        {
            case "roomPlayer" :     //需要转换          OK
                window.DKTools.audioEG.playBg("gameBgm");
                confige.specialType = false;
                confige.roomData = data;
                confige.roomPlayer = data.player;
                confige.roomId = data.roomId;
                // confige.gameSceneLoadOver = false;
                // confige.gameSceneLoadData = [];
                confige.playerMax = confige.roomData.playerNumber;
                if(confige.roomData.special)
                    confige.specialType = true;
                else
                    confige.specialType = false;
                window.clientGlobal.loadingNode.showLayer(1,"正在加载中......");
                if(confige.roomData.roomType == "sanKung")
                    cc.director.loadScene('GameSankung');
                else if(confige.roomData.roomType == "zhajinhua")
                    cc.director.loadScene('GameJinhua');
                else if(confige.roomData.roomType == "PaiGow")
                    cc.director.loadScene('GameScene1');
                else if(confige.roomData.roomType == "PaiGowBalcony"){
                    confige.isBalcony = true;
                    cc.director.loadScene('GameSceneBankerMode');
                }
                else
                    cc.director.loadScene('GameScene1');
                confige.curReconnectType = confige.ON_GAME;
                break;
            case "userInfo" :
                confige.specialType = false;
                console.log(data);
                confige.userInfo = data.data;
                confige.playerLimits = confige.userInfo.limits;
                confige.curDiamond = confige.userInfo.diamond;
                confige.curHistory = confige.userInfo.history;
                confige.curSex = parseInt(confige.userInfo.sex);
                window.clientGlobal.curSex = parseInt(confige.userInfo.sex);
                if(confige.loginType == 0)
                {
                    confige.curUseId = data.data.uid;
                }

                var newCallBack = function(){
                    if(confige.curReconnectType == confige.ON_LOGIN)
                    {
                        confige.curSceneIndex = 1;
                        confige.curReconnectType = confige.ON_HALL;
                    }
                    if(data.reconnection)       //是否有重连数据
                    {
                        window.DKTools.audioEG.playBg("gameBgm");
                        confige.curReconnectData = data.reconnection;
                        confige.roomData = data.reconnection.roomInfo;
                        confige.roomPlayer = data.reconnection.roomInfo.player;
                        confige.roomId = data.reconnection.roomInfo.roomId;
                        confige.playerMax = confige.roomData.playerNumber;
                        if(confige.roomData.special)
                            confige.specialType = true;
                        else
                            confige.specialType = false;
                        
                        if(confige.curReconnectType == confige.ON_HALL)
                        {
                            //自动跳转游戏场景并恢复数据
                            // confige.gameSceneLoadOver = false;
                            // confige.gameSceneLoadData = [];
                            window.clientGlobal.loadingNode.showLayer(1,"正在加载中......");
                            if(confige.roomData.roomType == "sanKung")
                                cc.director.loadScene('GameSankung');
                            else if(confige.roomData.roomType == "zhajinhua")
                                cc.director.loadScene('GameJinhua');
                            else if(confige.roomData.roomType == "PaiGow")
                                cc.director.loadScene('GameScene1');
                            else if(confige.roomData.roomType == "PaiGowBalcony"){
                                confige.isBalcony = true;
                                cc.director.loadScene('GameSceneBankerMode');
                            }
                            else
                                cc.director.loadScene('GameScene1');
                            confige.curReconnectType = confige.ON_GAME;
                        }else if(confige.curReconnectType == confige.ON_GAME){
                            //已经在游戏场景内,直接恢复数据
                            console.log("已经在游戏场景内,直接恢复数据");
                            pomelo.clientScene.gameInfoNode.btnClickRefresh();
                        }
                    }else{                      //没有则直接进入大厅界面
                        if(confige.curReconnectType != confige.ON_OVER)     //当处于结算界面时,不自动跳回大厅界面;
                        {
                            window.clientGlobal.loadingNode.showLayer(1,"正在加载中......");
                            cc.director.loadScene('HallScene');
                            confige.resetGameData();
                        }
                    }
                };

                if(confige.loginType == 1 || confige.loginType == 2)
                {
                    confige.getWXHearFrame(confige.userInfo.head, 0, newCallBack);
                }else{
                    newCallBack();
                }
                
                break;
            case "userJoin":        //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex == 2)
                    {
                        if(pomelo.clientScene.sceneLoadOver == true)
                            if(confige.roomData.roomType == "PaiGowBalcony")
                                pomelo.clientScene.addOnePlayer(data);
                            else
                                pomelo.clientScene.gamePlayerNode.addOnePlayer(data.player);
                    }
                }
                break;
            case "userQuit":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                     if(confige.curSceneIndex == 2)
                    {
                        pomelo.clientScene.gamePlayerNode.playerQuit(data.chair,true);
                    }
                }
               
                //data.uid  data.chair
                break;
            case "userReady" :      //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.onServerReady(confige.getCurChair(data.chair));      
                }
                break;
            case "deal" :           //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerDealCard(data);
                }
                
                break; 
            case "settlement" :
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    console.log(pomelo.clientScene);
                    if(pomelo.clientScene.gameBegin == true || confige.isBalcony)
                        pomelo.clientScene.onServerSettlement(data);
                    else
                        console.log("pomelo.clientScene.gameBegin == false");
                }
                break;
            case "beginBetting":    //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.onServerBeginBetting(data);
                }
                break;
            case "gameOver":
                confige.curReconnectType = confige.ON_OVER;
                if(pomelo.clientScene.gameBegin == true)
                {
                    pomelo.clientScene.gameInfoNode.showOverLayer(data);
                }else{
                    window.clientGlobal.loadingNode.showLayer(1,"正在加载中......");
                    cc.director.loadScene('HallScene');
                    if(confige.curGameScene.gameInfoNode.yuyinTimeOut != -1)
                    {
                        clearTimeout(confige.curGameScene.gameInfoNode.yuyinTimeOut);
                    }
                    confige.curGameScene.destroy();
                    confige.resetGameData();
                    if(confige.curUsePlatform == 1 || confige.curUsePlatform == 2)
                    {
                        confige.GVoiceCall.quitRoom(confige.GVoiceRoomID);
                        confige.GVoiceCall.closeListen();
                    }
                }
                break;
            case "beginRob":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.onServerRobBanker();
                }   
                break;
            case "robBanker":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.onServerRobBankerReturn(data);
                }
                break;
            case "banker":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.setBanker(data.chair);
                }   
                break;
            case "MingCard":        //需要转换
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.showMingCard(data.Cards);
                }
                break;
            case "showCard":        //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    pomelo.clientScene.gamePlayerNode.showOneCard(data.chair);
                    pomelo.clientScene.gamePlayerNode.noShowCardCount --;
                    console.log("pomelo.clientScene.noShowCardCount === " + pomelo.clientScene.gamePlayerNode.noShowCardCount);
                    if(pomelo.clientScene.gamePlayerNode.noShowCardCount <= 0){
                        pomelo.clientScene.timerItem.hideTimer();
                        console.log("hideTimer!!!!!");
                    }
                }
                break;
            case "bet":             //需要转换          OK
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    if(confige.isBalcony)
                        pomelo.clientScene.addBetNew(data.bet,confige.getCurChair(data.chair),confige.getCurChair(data.targetChair));
                    else
                        pomelo.clientScene.addBet(data.bet,confige.getCurChair(data.chair));
                }
                break;
            case "bonusPool":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    if(confige.curSceneIndex != 2)
                        break;
                    if(confige.roomData.roomType == "mingpaiqz")
                        pomelo.clientScene.showScorePool(data.bonusPool);
                    else
                        pomelo.clientScene.showScorePoolServer(data);
                }
                break; 
            case "sayMsg":
                if(confige.curSceneIndex != 2)
                    break;
                pomelo.clientScene.gameInfoNode.showSayWithMsg(data.chair,data.msg);
                break;
            //以下为炸金牛模式新增的协议
            case "curRound":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerZhaCall(data);
                }
                break;
            case "nextPlayer":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerZhaCall(data);
                }
                break;
            case "gameStart":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onNewGameStart();
                }
                break;
            case "gameBegin":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onNewGameBegin(data);
                }        
                break;
            case "lookCard":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerZhaCall(data);
                }
                break;
            case "compare":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerZhaCall(data);
                }
                break;
            case "giveUp":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.onServerZhaCall(data);
                }
                break;
            case "finishGame":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gameInfoNode.onServerShowFinish(data);
                break;
            case "responseFinish":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gameInfoNode.onServerResponseFinish(data);
                break;
            case "endFinish":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gameInfoNode.onServerEndFinish();
                break;
            case "updateDiamond":
                confige.curDiamond = data.data;
                if(confige.curSceneIndex == 1)
                    pomelo.clientScene.updateDiamond();
                break;
            case "updateHistory":
                confige.curHistory = data.data;
                if(confige.curSceneIndex == 1)
                    if(confige.curSceneIndex.historyLayer != -1)
                        pomelo.clientScene.updateHistory();
                break;
            case "downBanker":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else{
                    pomelo.clientScene.downBanker(data);
                }
                break;
            case "userDisconne":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gamePlayerNode.userDisconne(confige.getCurChair(data.chair));
                break;
            case "userReconnection":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gamePlayerNode.userReconnection(confige.getCurChair(data.chair));
                break;
            case "updatePlayerScore":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.gamePlayerNode.updateScoreByChair(data.chair,data.score);
                break;
            case "compareBet":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.compareBet(data.bet,confige.getCurChair(data.chair));
                break;
            case "readyBegin":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.readyBegin(data.waitTime);
                break;
            case "recover":
                pomelo.clientScene.gameInfoNode.btnClickRefresh();
                break;
            case "spectatorJoin":
                if(confige.gameSceneLoadOver == false){
                    confige.gameSceneLoadData.push(data);
                    console.log("spectatorJoin gameSceneLoadOver == false!!!!!")
                }
                else{
                    console.log("spectatorJoin gameSceneLoadOver == true!!!!!!!")
                    pomelo.clientScene.serverVistorJoinOrLeave(data,1);
                }
                break;
            case "spectatorLeave":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.serverVistorJoinOrLeave(data,0);
                break;
            case "sitDown":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.serverSitDown(data);
                break;
            case "managerChangeScore":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.serverChangeScore(data);
                break;
            case "standUp":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.serverStandUp(data);
                break;
            case "changeKillPoints":
                if(confige.gameSceneLoadOver == false)
                    confige.gameSceneLoadData.push(data);
                else
                    pomelo.clientScene.changeKillPoints(data.killPoints);
                break;
        }
    };
            
    pomelo.on("onNotify",function(data) {
        console.log("onNotify")
        console.log(data);
        pomelo.dealWithOnNotify(data);
        
    });

    pomelo.dealWithOnNotify = function(data){
        switch(data.type)
        {
            case "broadcast" :
                for(var i =0;i<3;i++)
                {
                    pomelo.clientScene.paomaAddOneText(data.content);
                }
                break;
            case "rolling":
                // for(var i=0;i<data.count;i++)
                if(confige.curSceneIndex == 1)
                    pomelo.clientScene.paomaAddOneText(data.content,data.count);
                else
                    confige.hallSceneLoadData.push(data);
                break;
            case "notify":
                if(confige.curSceneIndex == 1)
                {
                    pomelo.clientScene.noticeLayer.addNotice(data.data);
                    pomelo.clientScene.noticeLayer.showLayer();
                }else
                    confige.hallSceneLoadData.push(data);
                break;
        }
    };

    pomelo.clientSend = function(code,msg,cbTrue,cbFalse) {
            pomelo.request("connector.entryHandler.sendData", {"code" : code,"params" : msg}, function(data) {
                console.log("on send!!!");
                console.log(data);
                if(data.code && data.code == 119)
                {
                    pomelo.disconnect();
                    return;
                }
                if(data.flag == false)
                {
                    if(cbFalse)
                        cbFalse();
                    console.log(data);
                    if(code == "join")
                    {
                        pomelo.clientScene.hideReturnRoom();
                        console.log("join room ???")
                        if(data.msg && data.msg.code)
                        {
                            pomelo.clientScene.showTips(tipsConf[data.msg.code], 2);
                        }else{
                            pomelo.clientScene.showTips("加入房间失败,请重新输入!", 2);
                        }
                    }
                }else{
                    console.log("do clientSend cbTrue!!!!!!");
                    if(cbTrue)
                        cbTrue();
                }
            }
        );
    };

    pomelo.clientCreateRoom = function(GameMode, BankerMode, ConsumeMode, GameNum, CardMode, PlayerNum, GameType, BasicScore, BasicType, CreateType, HalfwayEnter, AllowAllin,AllowAward,AllowWait, AllowSpecial, cbTrue,cbFalse) {
            console.log("on create room!")
            var createType = CreateType;

            if(GameType == "mingpaiqz")
            {
                pomelo.request("connector.entryHandler.sendData", {"code" : createType,"params" : {gameMode: GameMode,
                    bankerMode: BankerMode, consumeMode: ConsumeMode, gameNumber: GameNum, cardMode: CardMode, playerNumber: PlayerNum, gameType: GameType, basicType:BasicType, basic:BasicScore, halfwayEnter: HalfwayEnter,allowAllin:AllowAllin,limitAward:AllowAward,waitMode:AllowWait,special:AllowSpecial}}, function(data) {
                        console.log("clientCreateRoom flag is : " + data.flag)
                        console.log(data);
                        if(data.code && data.code == 119)
                        {
                            pomelo.disconnect();
                            return;
                        }
                        if(data.flag == false)
                        {
                            if(cbFalse)
                                cbFalse();
                            if(data.msg && data.msg.code)
                            {
                                console.log("创建房间失败,有code@@@@@");
                                pomelo.clientScene.showTips(tipsConf[data.msg.code]);
                            }else{
                                console.log("创建房间失败,没有code@@@@@");
                                pomelo.clientScene.showTips("创建房间失败,请重新创建!");
                            }
                        }else{
                            console.log("data.flag == true");
                            if(cbTrue)
                                cbTrue();
                            if(createType == "newRoom")
                                if(data.msg && data.msg.roomId)
                                    pomelo.clientScene.showTips("创建房间成功!\n房间号为:" + data.msg.roomId, 1);
                        }
                    }
                );
            }else{
                pomelo.request("connector.entryHandler.sendData", {"code" : createType,"params" : {gameMode: GameMode,
                    bankerMode: BankerMode, consumeMode: ConsumeMode, gameNumber: GameNum, cardMode: CardMode, playerNumber: PlayerNum, gameType: GameType,basicType:BasicScore,basic: BasicScore, halfwayEnter: HalfwayEnter,allowAllin:AllowAllin,limitAward:AllowAward,waitMode:AllowWait,special:AllowSpecial}}, function(data) {
                        console.log("clientCreateRoom flag is : " + data.flag)
                        console.log(data);
                        if(data.code && data.code == 119)
                        {
                            pomelo.disconnect();
                            return;
                        }
                        if(data.flag == false)
                        {
                            if(cbFalse)
                                cbFalse();
                            if(data.msg && data.msg.code)
                            {
                                console.log("创建房间失败,有code@@@@@");
                                pomelo.clientScene.showTips(tipsConf[data.msg.code]);
                            }else{
                                console.log("创建房间失败,没有code@@@@@");
                                pomelo.clientScene.showTips("创建房间失败,请重新创建!");
                            }
                        }else{
                            console.log("data.flag == true");
                            if(cbTrue)
                                cbTrue();
                            if(createType == "newRoom")
                                if(data.msg && data.msg.roomId)
                                    pomelo.clientScene.showTips("创建房间成功!\n房间号为:" + data.msg.roomId, 1);
                        }
                    }
                );
            }
    };

    confige.host = "120.55.61.144"; //test
    // confige.host = "qj.qhitsf.com";     //运营外网
    // confige.host = "192.168.1.65";          //内网
    // confige.host = "127.0.0.1";          //本机
    // confige.host = "192.168.43.17";          //内网2
    pomelo.clientLogin = function(uid,clientLogintoken) {
        console.log("pomelo try to login!!!!!!");
        var route = 'gate.gateHandler.queryEntry';
        pomelo.init({
          host: confige.host,
          port: "3900",
          log: true
        }, function() {
            
            pomelo.request(route, function(data) {
                pomelo.disconnect();
                if(data.code === 500) {
                  console.log(data)
                  return;
                }
                // confige.host = data.host;
                confige.port = data.port;
                confige.curUseId = uid;
                if(clientLogintoken)
                    confige.curUseToken = clientLogintoken;
                if(confige.loginType == 0)
                {
                    pomelo.loginWithHostAndPort(confige.host,data.port,uid);
                }else if(confige.loginType == 1){
                    pomelo.loginWithHostAndPort(confige.host,data.port,uid,clientLogintoken);
                }else if(confige.loginType == 2){
                    pomelo.loginWithHostAndPort(confige.host,data.port,-1,-1,confige.curUseCode);
                }
            });
        });
    };
    pomelo.connectCount = 10;
    pomelo.loginWithHostAndPort = function(host, port, uid, token, code){
        pomelo.init({
                host: host,
                port: port,
                log: true
            }, function() {
            var route = "connector.entryHandler.visitorEnter";
            //注册事件
            if(confige.loginFirstTime == true)
            {
                confige.loginFirstTime = false;
                pomelo.on("disconnect",function(reason) {
                    console.log("重连中!!!!!!");
                    // pomelo.disconnect();
                });
                pomelo.on("close",function(){
                    console.log("你断线了!!!!!");
                    var route = 'gate.gateHandler.queryEntry';
                
                    if(confige.curUsePlatform == 3){
                        if(window.clientGlobal.tipsBox){
                            window.clientGlobal.tipsBox.showLayer(1,"登录验证失败,请重新登录!",function(){
                                window.open(confige.h5LoginUrl);
                            });
                        }else{
                            console.log("1312312");
                            window.open(confige.h5LoginUrl);
                            // window.close();
                        }
                        return;
                    }
                    console.log("你断线了!!!!!");
                    if(confige.curSceneIndex != 0 && window.clientGlobal.loadingNode)
                        window.clientGlobal.loadingNode.showLayer();
                    if(pomelo.connectCount > 0)
                    {
                        pomelo.connectCount --;
                        setTimeout(function(){
                            pomelo.reConnet();
                        },1000);
                    }else{
                        console.log("强制关闭游戏,停止重连");
                        window.clientGlobal.tipsBox.showLayer(1,"网络异常,请重新登录!",function(){
                            cc.game.restart();
                        });
                    }
                });
            }
            //转接登录逻辑
            if(confige.loginType == 0)
            {
                route = "connector.entryHandler.visitorEnter";
                pomelo.request(route,{"uid" : uid}, function(data) {
                    if(data.error) {
                        console.log(data)
                        return;
                    }
                    if(data.flag == true)
                    {
                        if(window.clientGlobal.loadingNode && confige.curReconnectType == confige.ON_OVER)
                            window.clientGlobal.loadingNode.hideLayer();
                        pomelo.connectCount = 10;
                    }
                });
            }else if(confige.loginType == 1){
                route = "connector.entryHandler.enter";
                pomelo.request(route,{"openId" : uid, "token" : token, "version" : confige.versionCheck, "platform":confige.curUsePlatform}, function(data) {
                    if(data.error) {
                        console.log(data)
                        return;
                    }
                    pomelo.clientScene.connectCallBack();
                    if(data.code == -120)
                    {
                        console.log("版本不对");
                        window.clientGlobal.tipsBox.showLayer(0,"当前版本较低,请前往官网下载最新版本!");
                    }
                    if(data.flag == true){
                        pomelo.connectCount = 10;
                        confige.curUseId = uid;
                        confige.curUseToken = token;
                        if(window.clientGlobal.loadingNode && confige.curReconnectType == confige.ON_OVER)
                            window.clientGlobal.loadingNode.hideLayer();
                    }
                });
            }else if(confige.loginType == 2){
                route = "connector.entryHandler.h5Enter";
                pomelo.request(route,{"code" : code,"version" : confige.versionCheck, "platform":confige.curUsePlatform}, function(data) {
                    console.log("h5Login！！！！！！！！")
                    console.log(data);
                    if(data.flag == false) {
                        console.log("登录失败,刷新页面!!!!!!")
                        if(window.clientGlobal.tipsBox){
                            window.clientGlobal.tipsBox.showLayer(1,"登录验证失败,请重新登录!",function(){
                                window.open(confige.h5LoginUrl);
                            });
                        }else{
                            console.log("1312312");
                            window.open(confige.h5LoginUrl);
                            // window.close();
                        }
                        return;
                    }
                    pomelo.clientScene.connectCallBack();
                    if(data.code == -120)
                    {
                        console.log("版本不对");
                        window.clientGlobal.tipsBox.showLayer(0,"当前版本较低,请前往官网下载最新版本!");
                    }
                    if(data.flag == true){
                        pomelo.request("connector.entryHandler.getTicket",null, function(data) {
                            console.log(data);
                            if(data.flag == true)
                            {
                                confige.h5SignTicket = data.jsapi_ticket;

                                console.log("wx.config begin!!!!!!!!!!!!!!!!!!");
                                confige.h5SignTime = Date.parse(new Date());
                                console.log("完整路径3333===="+confige.h5SignURL);
                                var originString = "jsapi_ticket="+confige.h5SignTicket+"&noncestr="+confige.h5SignStr+"&timestamp="+confige.h5SignTime+"&url="+confige.h5SignURL;
                                console.log("originString==="+originString);
                                var encodeString = hex_sha1(originString);
                                console.log("encodeString==="+encodeString);
                                confige.h5SignSignature = encodeString;
                                wx.config({
                                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                                    appId: confige.h5SignID, // 必填，企业号的唯一标识，此处填写企业号corpid
                                    timestamp: confige.h5SignTime, // 必填，生成签名的时间戳
                                    nonceStr: confige.h5SignStr, // 必填，生成签名的随机串
                                    signature: confige.h5SignSignature,// 必填，签名，见附录1
                                    jsApiList: [    "onMenuShareTimeline",
                                                    "onMenuShareAppMessage",
                                                    "chooseImage"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                                });
                                console.log("wx.config end!!!!!!!!!!!!!!!!!!!!");

                            }
                            if(data.flag == false)
                                console.log("服务器出大问题了!!!!!!!!!");
                        });

                        pomelo.connectCount = 3;
                        // if(window.clientGlobal.loadingNode)
                            // window.clientGlobal.loadingNode.hideLayer();
                    }
                });
            }
        });
    };        

    pomelo.reConnet = function(){
        console.log("pomelo.reConnet===="+confige.curReconnectType);
        if(confige.curReconnectType == confige.ON_LOGIN){
            console.log("confige.curReconnectType == confige.ON_LOGIN!!!!!!!!!!!!!!!!");
            return;
        }
        if(confige.loginType == 0)
            pomelo.loginWithHostAndPort(confige.host,confige.port,confige.curUseId);
        else if(confige.loginType == 1)
            pomelo.loginWithHostAndPort(confige.host,confige.port,confige.curUseId,confige.curUseToken);
        else if(confige.loginType == 2)
            pomelo.loginWithHostAndPort(confige.host,confige.port,-1,-1,confige.curUseCode);
        else if(confige.curUsePlatform == 3){
            window.open(confige.h5LoginUrl);
            // window.close();
        }
    };

};

module.exports = pClient;